package roadsuggester.testing;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import roadsuggester.main.DestinationProximityCalculator;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;


//Mock location provider for testing.
public class MockLocationProvider extends Thread
{
	private LocationManager locationManager;
	private String mocLocationProvider;
	private InputStream dataInputStream;

	public MockLocationProvider(Context context, LocationManager locationManager, String mocLocationProvider, InputStream dataInputStream)
	{

		this.locationManager=locationManager;
		this.mocLocationProvider=mocLocationProvider;
		this.dataInputStream=dataInputStream;
	}

	@Override
	public void run()
	{
		List<String> dataLines=new ArrayList<String>();

		try
		{
			BufferedReader reader=new BufferedReader(new InputStreamReader(dataInputStream));
			String line=null;
			while((line=reader.readLine())!=null)
			{
				dataLines.add(line);
			}

		}
		catch(IOException e)
		{

			e.printStackTrace();
		}

		long lastMillis=0;
		for(String dataLine: dataLines)
		{
			// Set one position
			String[] parts=dataLine.split(" ");
			Double latitude=Double.valueOf(parts[0]);
			Double longitude=Double.valueOf(parts[1]);
			double seconds=Double.valueOf(parts[2]);
			long millis=(long) (seconds*1000);

			Location location=new Location(mocLocationProvider);
			location.setLatitude(latitude);
			location.setLongitude(longitude);


			// set the time in the location. If the time on this location
			// matches the time on the one in the previous set call, it will be
			// ignored
			location.setTime(System.currentTimeMillis()+millis);

			try
			{
				Thread.sleep(millis-lastMillis);
			}
			catch(InterruptedException e)
			{
			}
			lastMillis=millis;

			locationManager.setTestProviderLocation(mocLocationProvider, location);
		}
	}
}